<template>
  <el-container style="padding: 15px">
    <el-header height="120px">
      <el-form size="small" label-width="80px">
        <el-row type="flex" align="middle">
          <el-col :span="6">
            <el-form-item label="不动产类型" label-width="90px">
              <el-select v-model="searchObj.projectId">
                <el-option v-for="item in projectList" :key="item.id" :label="item.projectName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="估价目的">
              <el-select v-model="searchObj.objectiveId">
                <el-option v-for="item in objectiveList" :key="item.id" :label="item.reportObjectiveName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="middle">
          <el-col :span="6">
            <el-form-item label="用途">
              <el-select v-model="searchObj.useId">
                <el-option v-for="item in useList" :key="item.id" :label="item.reportUseName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="报告类型">
              <el-select v-model="searchObj.reportTypeId">
                <el-option v-for="item in typeList" :key="item.id" :label="item.reportTypeName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目状态">
              <el-select v-model="searchObj.reportLeafId">
                <el-option v-for="item in projectStatusList" :key="item.id" :label="item.leafName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="getPage">搜索</el-button>
              <el-button @click="resetPage">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-table v-loading="listLoading" :data="auditList">
        <el-table-column label="不动产类型" prop="projectName" width="90px" />
        <el-table-column label="项目编号" prop="reportId" width="120px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" prop="reportName" width="300px" :formatter="subName" />
        <el-table-column label="用途" prop="reportUseName" width="60px" />
        <el-table-column label="估价目的" prop="reportObjectiveName" width="80px" />
        <el-table-column label="报告类型" prop="reportTypeName" />
        <el-table-column label="项目状态" prop="leafName" width="140px" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="carryAudit(scope.row)">承接审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { validObj } from '@/utils/validate'
import { pageAudit, searchFlowPoolPage, searchFormCacheListAudit, updateReportLeaf } from '@/api/flow'
import { mapGetters } from 'vuex'
import { ellipsisString } from '@/utils'

export default {
  name: 'Index',
  data() {
    return {
      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      auditList: [],

      projectList: [],
      objectiveList: [],
      useList: [],
      typeList: [],
      projectStatusList: [],

      searchObj: {},

      listLoading: false
    }
  },

  created() {
    this.init()
  },

  computed: {
    ...mapGetters([
      'id'
    ])
  },

  methods: {
    init() {
      this.getPage()
      this.getFormList()
    },
    getPage() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      this.listLoading = true
      if (validObj(searchObj)) {
        // 如果不为空则搜索
        searchFlowPoolPage(searchObj, null, currentPage, sizePage).then(res => {
          this.auditList = res.data.list
          this.totalPage = res.data.count
        }).finally(() => {
          this.listLoading = false
        })
      } else {
        pageAudit(currentPage, sizePage).then(res => {
          this.auditList = res.data.record
          this.totalPage = res.data.total
        }).finally(() => {
          this.listLoading = false
        })
      }
    },
    handleSizeChange(val) {
      this.sizePage = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getPage()
    },

    toDetails(id) {
      this.$router.push({ path: '/report/details', query: { id: id }})
    },

    getFormList() {
      searchFormCacheListAudit().then(res => {
        const data = res.data
        this.projectList = data.projects
        this.objectiveList = data.objectives
        this.useList = data.uses
        this.typeList = data.types
        this.projectStatusList = data.statuses
      })
    },

    carryAudit(data) {
      this.$confirm('此操作将改变当前状态，并且自动生成审核记录，确认继续吗？', '承接审核', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        const reportLeaf = {
          reportId: data.id,
          leafId: data.leafId,
          auditUserId: this.id
        }
        console.log(reportLeaf)
        updateReportLeaf(reportLeaf, 2).then(res => {
          this.$message.success(res.message)
          this.$router.push({ path: '/report/details', query: { id: data.id }})
        })
      })
    },

    resetPage() {
      this.searchObj = {}
      this.currentPage = 1
      this.sizePage = 10
      this.getPage()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },

    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    }
  }
}
</script>

<style scoped>

</style>
